using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MSOutlook = Microsoft.Office.Interop.Outlook;
using Utility = Microsoft.Sample.Outlook.Utility;
using System.Diagnostics;

namespace Microsoft.Sample.Outlook.CRMIntegration.FolderHomePages
{
	[System.Runtime.InteropServices.ComVisible(true)]
	[System.Runtime.InteropServices.Guid("5611FC70-D5B7-446c-A225-48F3EA957F1F")]
	public sealed partial class AccountToday : UserControl
	{
#if !INSTALL
        //These registration methods are only called when compiling w/ VS
        //For installer scenarios, the RegisterSafeforScripting call is 
        //executed by the installer class
		[System.Runtime.InteropServices.ComRegisterFunction()]
		public static void RegisterServer(Type t)
		{
			Utility.FolderHomePage.RegisterSafeForScripting(t);
		}

		[System.Runtime.InteropServices.ComUnregisterFunction()]
		public static void UnregisterServer(Type t)
		{
			Utility.FolderHomePage.UnregisterSafeForScripting(t);
		}
#endif

		MSOutlook.Application _app;

		public void Initialize(MSOutlook.Application o)
		{
			_app = (MSOutlook.Application)o;

			MSOutlook.Explorer activeExplorer = _app.ActiveExplorer();

			string rootFolder = activeExplorer.CurrentFolder.FolderPath + "\\";

			InitOutlookView(rootFolder + Properties.Resources.ActivitiesFolderName, tabPageActivities);
			InitOutlookView(rootFolder + Properties.Resources.ContactsFolderName, tabPageContacts);
			InitOutlookView(rootFolder + Properties.Resources.OpportunitiesFolderName, tabPageOpportunities);

			try
			{
				accountsTableAdapter.FillByOutlookID(crmDataSet.Accounts, activeExplorer.CurrentFolder.EntryID);
			}
			catch (Exception ex)
			{
                MessageBox.Show("The AccountToday page couldn't access the database", "Outlook CRM Integration Sample");
                ThisApplication.LogException(ex);

			}

		}

		private static void InitOutlookView(string folderName, TabPage tab)
		{
			Utility.OutlookViewControl view = new Utility.OutlookViewControl();
			view.Dock = DockStyle.Fill;
			view.Folder = folderName;
			tab.Controls.Add(view);
		}

		public AccountToday()
		{
			InitializeComponent();
			ThisApplication.ConfigureDataDirectory(this.GetType());
		}

		private void accountsBindingSource_CurrentItemChanged(object sender, EventArgs e)
		{
			labelCityStateZip.Text = string.Empty;

			DataRowView view = (DataRowView)accountsBindingSource.Current;
			CrmDataSet.AccountsRow row = (CrmDataSet.AccountsRow)view.Row;

			string city = string.Empty;
			if (!row.IsCityNull())
				city = row.City + ", ";

			string state = string.Empty;
			if (!row.IsStateRegionNull())
				state = row.StateRegion + " ";

			string zip = string.Empty;
			if (!row.IsPostalCodeNull())
				zip = row.PostalCode;

			labelCityStateZip.Text = city + state + zip;
		}

        private void labelCompanyWebSiteValue_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo sInfo = new ProcessStartInfo(((LinkLabel)sender).Text);
            Process.Start(sInfo);
        }

       
	}
}
